package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.datatransferobject.WeiboAddParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.valueobject.UserVO;
import cn.tedu._04weibo.pojo.valueobject.WeiboDetailVO;
import cn.tedu._04weibo.pojo.valueobject.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
@Tag(name = "02-微博模块")
@Slf4j
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    @Operation(summary = "01-发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult addWeiboParam(@RequestBody WeiboAddParam weiboAddParam, HttpSession session) {
        //log.debug("content:" + content);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboAddParam, weibo);
            weibo.setCreateTime(new Date());
            weibo.setUserId(userVO.getId());
            int num = weiboMapper.addWeibo(weibo);
            if (num > 0) {
                return JsonResult.Bingo();
            } else {
                return new JsonResult(StatusCode.OPERATION_FAILED);
            }
//        }
    }

    @PostMapping("selectIndex")
    @Operation(summary = "02-微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectWeibo(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            List<WeiboIndexVO> weiboIndexVO = weiboMapper.listWeibo();
            return JsonResult.Bingo(weiboIndexVO);
//        }
    }

    @GetMapping("selectById")
    @Operation(summary = "03-微博详情页")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "微博编号", required = true)
    public JsonResult selectDetail(Long id, HttpSession session) {
        log.debug("id:" + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            WeiboDetailVO weiboDetailVO = weiboMapper.seDetail(id);
            return JsonResult.Bingo(weiboDetailVO);
//        }
    }
}
